home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / mpeg_play-2.1 / 16bit.c next >
Encoding:
C/C++ Source or Header  |  1995-05-09  |  18.7 KB  |  797 lines

  1. /*
  2.  * Copyright (c) 1995 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. /*
  23.  * Copyright (c) 1995 Erik Corry
  24.  * All rights reserved.
  25.  * 
  26.  * Permission to use, copy, modify, and distribute this software and its
  27.  * documentation for any purpose, without fee, and without written agreement is
  28.  * hereby granted, provided that the above copyright notice and the following
  29.  * two paragraphs appear in all copies of this software.
  30.  * 
  31.  * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  32.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
  33.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
  34.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  * 
  36.  * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  37.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  38.  * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  39.  * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
  40.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  41.  */
  42.  
  43. #include "video.h"
  44. #include "dither.h"
  45. #include "proto.h"
  46.  
  47. #define INTERPOLATE
  48.  
  49. /*
  50.  * Erik Corry's multi-byte dither routines.
  51.  *
  52.  * The basic idea is that the Init generates all the necessary tables.
  53.  * The tables incorporate the information about the layout of pixels
  54.  * in the XImage, so that it should be able to cope with 15-bit, 16-bit
  55.  * 24-bit (non-packed) and 32-bit (10-11 bits per color!) screens.
  56.  * At present it cannot cope with 24-bit packed mode, since this involves
  57.  * getting down to byte level again. It is assumed that the bits for each
  58.  * color are contiguous in the longword.
  59.  * 
  60.  * Writing to memory is done in shorts or ints. (Unfortunately, short is not
  61.  * very fast on Alpha, so there is room for improvement here). There is no
  62.  * dither time check for overflow - instead the tables have slack at
  63.  * each end. This is likely to be faster than an 'if' test as many modern
  64.  * architectures are really bad at ifs. Potentially, each '&&' causes a 
  65.  * pipeline flush!
  66.  *
  67.  * There is no shifting and fixed point arithmetic, as I really doubt you
  68.  * can see the difference, and it costs. This may be just my bias, since I
  69.  * heard that Intel is really bad at shifting.
  70.  */
  71.  
  72. /*
  73.  * How many 1 bits are there in the longword.
  74.  * Low performance, do not call often.
  75.  */
  76. static int
  77. number_of_bits_set(unsigned long a)
  78. {
  79.     if(!a) return 0;
  80.     if(a & 1) return 1 + number_of_bits_set(a >> 1);
  81.     return(number_of_bits_set(a >> 1));
  82. }
  83.  
  84. /*
  85.  * Shift the 0s in the least significant end out of the longword.
  86.  * Low performance, do not call often.
  87.  */
  88. static unsigned long
  89. shifted_down(unsigned long a)
  90. {
  91.     if(!a) return 0ul;
  92.     if(a & 1) return a;
  93.     return a >> 1;
  94. }
  95.  
  96. /*
  97.  * How many 0 bits are there at most significant end of longword.
  98.  * Low performance, do not call often.
  99.  */
  100. static int
  101. free_bits_at_top(unsigned long a)
  102. {
  103.       /* assume char is 8 bits */
  104.     if(!a) return sizeof(unsigned long) * 8;
  105.         /* assume twos complement */
  106.     if(((long)a) < 0l) return 0;
  107.     return 1 + free_bits_at_top ( a << 1);
  108. }
  109.  
  110. /*
  111.  * How many 0 bits are there at least significant end of longword.
  112.  * Low performance, do not call often.
  113.  */
  114. static int
  115. free_bits_at_bottom(unsigned long a)
  116. {
  117.       /* assume char is 8 bits */
  118.     if(!a) return sizeof(unsigned long) * 8;
  119.     if(((long)a) & 1l) return 0;
  120.     return 1 + free_bits_at_bottom ( a >> 1);
  121. }
  122.  
  123. static int *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
  124.  
  125. /*
  126.  * We define tables that convert a color value between -256 and 512
  127.  * into the R, G and B parts of the pixel. The normal range is 0-255.
  128.  */
  129.  
  130. static long *r_2_pix;
  131. static long *g_2_pix;
  132. static long *b_2_pix;
  133. static long *r_2_pix_alloc;
  134. static long *g_2_pix_alloc;
  135. static long *b_2_pix_alloc;
  136.  
  137.  
  138.  
  139. /*
  140.  *--------------------------------------------------------------
  141.  *
  142.  * InitColor16Dither --
  143.  *
  144.  *    To get rid of the multiply and other conversions in color
  145.  *    dither, we use a lookup table.
  146.  *
  147.  * Results:
  148.  *    None.
  149.  *
  150.  * Side effects:
  151.  *    The lookup tables are initialized.
  152.  *
  153.  *--------------------------------------------------------------
  154.  */
  155.  
  156. void
  157. InitColorDither(thirty2)
  158. int thirty2;
  159. {
  160.     /*
  161.      * misuse of the wpixel array for the pixel masks. Note that this
  162.      * implies that the window is created before this routine is called
  163.      */
  164.     unsigned long red_mask = wpixel[0];
  165.     unsigned long green_mask = wpixel[1];
  166.     unsigned long blue_mask = wpixel[2];
  167.  
  168.     int L, CR, CB, i;
  169.  
  170.     L_tab    = (int *)malloc(256*sizeof(int)); 
  171.     Cr_r_tab = (int *)malloc(256*sizeof(int));
  172.     Cr_g_tab = (int *)malloc(256*sizeof(int));
  173.     Cb_g_tab = (int *)malloc(256*sizeof(int));
  174.     Cb_b_tab = (int *)malloc(256*sizeof(int));
  175.  
  176.     r_2_pix_alloc = (long *)malloc(768*sizeof(long));
  177.     g_2_pix_alloc = (long *)malloc(768*sizeof(long));
  178.     b_2_pix_alloc = (long *)malloc(768*sizeof(long));
  179.  
  180.     for (i=0; i<256; i++) {
  181.       L = i - 16;
  182.       L_tab[i] = 1.164 * L;
  183.       if (gammaCorrectFlag) {
  184.         L_tab[i] = pow(L_tab[i] / 255.0, 1 / gammaCorrect) * 255.0 + 0.5;
  185.       }
  186.  
  187.       CB = CR = i;
  188.  
  189.       CB -= 128; CR -= 128;
  190. /* was
  191.       Cr_r_tab[i] =  1.596 * CR;
  192.       Cr_g_tab[i] = -0.813 * CR;
  193.       Cb_g_tab[i] = -0.391 * CB;   
  194.       Cb_b_tab[i] =  2.018 * CB;
  195. */
  196.       Cr_r_tab[i] =  1.366 * CR;
  197.       Cr_g_tab[i] = -0.700 * CR;
  198.       Cb_g_tab[i] = -0.334 * CB;   
  199.       Cb_b_tab[i] =  1.732 * CB;
  200.  
  201.     }
  202.  
  203.     /* 
  204.      * Set up entries 0-255 in rgb-to-pixel value tables.
  205.      */
  206.     for (i = 0; i < 256; i++) {
  207.       r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(red_mask));
  208.       r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(red_mask);
  209.       g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(green_mask));
  210.       g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(green_mask);
  211.       b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(blue_mask));
  212.       b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(blue_mask);
  213.       /*
  214.        * If we have 16-bit output depth, then we double the value
  215.        * in the top word. This means that we can write out both
  216.        * pixels in the pixel doubling mode with one op. It is 
  217.        * harmless in the normal case as storing a 32-bit value
  218.        * through a short pointer will lose the top bits anyway.
  219.        * A similar optimisation for Alpha for 64 bit has been
  220.        * prepared for, but is not yet implemented.
  221.        */
  222.       if(!thirty2) {
  223.  
  224.     r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 16;
  225.     g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 16;
  226.     b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 16;
  227.  
  228.       }
  229. #ifdef SIXTYFOUR_BIT
  230.       if(thirty2) {
  231.  
  232.     r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 32;
  233.     g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 32;
  234.     b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 32;
  235.  
  236.       }
  237. #endif
  238.     }
  239.  
  240.     /*
  241.      * Spread out the values we have to the rest of the array so that
  242.      * we do not need to check for overflow.
  243.      */
  244.     for (i = 0; i < 256; i++) {
  245.       r_2_pix_alloc[i] = r_2_pix_alloc[256];
  246.       r_2_pix_alloc[i+ 512] = r_2_pix_alloc[511];
  247.       g_2_pix_alloc[i] = g_2_pix_alloc[256];
  248.       g_2_pix_alloc[i+ 512] = g_2_pix_alloc[511];
  249.       b_2_pix_alloc[i] = b_2_pix_alloc[256];
  250.       b_2_pix_alloc[i+ 512] = b_2_pix_alloc[511];
  251.     }
  252.  
  253.     r_2_pix = r_2_pix_alloc + 256;
  254.     g_2_pix = g_2_pix_alloc + 256;
  255.     b_2_pix = b_2_pix_alloc + 256;
  256.  
  257. }
  258.  
  259.  
  260. /*
  261.  *--------------------------------------------------------------
  262.  *
  263.  * Color16DitherImage --
  264.  *
  265.  *    Converts image into 16 bit color.
  266.  *
  267.  * Results:
  268.  *    None.
  269.  *
  270.  * Side effects:
  271.  *    None.
  272.  *
  273.  *--------------------------------------------------------------
  274.  */
  275.  
  276. void
  277. Color16DitherImage(lum, cr, cb, out, rows, cols)
  278.   unsigned char *lum;
  279.   unsigned char *cr;
  280.   unsigned char *cb;
  281.   unsigned char *out;
  282.   int cols, rows;
  283.  
  284. {
  285.     int L, CR, CB;
  286.     unsigned short *row1, *row2;
  287.     unsigned char *lum2;
  288.     int x, y;
  289.     unsigned int r, b, g;
  290.     int cr_r;
  291.     int cr_g;
  292.     int cb_g;
  293.     int cb_b;
  294.     int cols_2 = cols/2;
  295.  
  296.     row1 = (unsigned short *)out;
  297.     row2 = row1 + cols_2 + cols_2;
  298.     lum2 = lum + cols_2 + cols_2;
  299.  
  300.     for (y=0; y<rows; y+=2) {
  301.     for (x=0; x<cols_2; x++) {
  302.         int R, G, B;
  303.  
  304.         CR = *cr++;
  305.         CB = *cb++;
  306.         cr_r = Cr_r_tab[CR];
  307.         cr_g = Cr_g_tab[CR];
  308.         cb_g = Cb_g_tab[CB];
  309.         cb_b = Cb_b_tab[CB];
  310.  
  311.             L = L_tab[(int) *lum++];
  312.  
  313.         R = L + cr_r;
  314.         G = L + cr_g + cb_g;
  315.         B = L + cb_b;
  316.  
  317.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  318.  
  319. #ifdef INTERPOLATE
  320.             if(x != cols_2 - 1) {
  321.           CR = (CR + *cr) >> 1;
  322.           CB = (CB + *cb) >> 1;
  323.           cr_r = Cr_r_tab[CR];
  324.           cr_g = Cr_g_tab[CR];
  325.           cb_g = Cb_g_tab[CB];
  326.           cb_b = Cb_b_tab[CB];
  327.             }
  328. #endif
  329.  
  330.             L = L_tab[(int) *lum++];
  331.  
  332.         R = L + cr_r;
  333.         G = L + cr_g + cb_g;
  334.         B = L + cb_b;
  335.  
  336.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  337.  
  338.         /*
  339.          * Now, do second row.
  340.          */
  341. #ifdef INTERPOLATE
  342.             if(y != rows - 2) {
  343.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  344.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  345.           cr_r = Cr_r_tab[CR];
  346.           cr_g = Cr_g_tab[CR];
  347.           cb_g = Cb_g_tab[CB];
  348.           cb_b = Cb_b_tab[CB];
  349.             }
  350. #endif
  351.  
  352.         L = L_tab[(int) *lum2++];
  353.         R = L + cr_r;
  354.         G = L + cr_g + cb_g;
  355.         B = L + cb_b;
  356.  
  357.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  358.  
  359.         L = L_tab[(int) *lum2++];
  360.         R = L + cr_r;
  361.         G = L + cr_g + cb_g;
  362.         B = L + cb_b;
  363.  
  364.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  365.     }
  366.         /*
  367.          * These values are at the start of the next line, (due
  368.          * to the ++'s above),but they need to be at the start
  369.          * of the line after that.
  370.          */
  371.     lum += cols_2 + cols_2;
  372.     lum2 += cols_2 + cols_2;
  373.     row1 += cols_2 + cols_2;
  374.     row2 += cols_2 + cols_2;
  375.     }
  376. }
  377.  
  378.  
  379.  
  380.  
  381. /*
  382.  *--------------------------------------------------------------
  383.  *
  384.  * Color32DitherImage --
  385.  *
  386.  *    Converts image into 32 bit color (or 24-bit non-packed).
  387.  *
  388.  * Results:
  389.  *    None.
  390.  *
  391.  * Side effects:
  392.  *    None.
  393.  *
  394.  *--------------------------------------------------------------
  395.  */
  396.  
  397. /*
  398.  * This is a copysoft version of the function above with ints instead
  399.  * of shorts to cause a 4-byte pixel size
  400.  */
  401.  
  402. void
  403. Color32DitherImage(lum, cr, cb, out, rows, cols)
  404.   unsigned char *lum;
  405.   unsigned char *cr;
  406.   unsigned char *cb;
  407.   unsigned char *out;
  408.   int cols, rows;
  409.  
  410. {
  411.     int L, CR, CB;
  412.     unsigned int *row1, *row2;
  413.     unsigned char *lum2;
  414.     int x, y;
  415.     unsigned int r, b, g;
  416.     int cr_r;
  417.     int cr_g;
  418.     int cb_g;
  419.     int cb_b;
  420.     int cols_2 = cols / 2;
  421.  
  422.     row1 = (unsigned int *)out;
  423.     row2 = row1 + cols_2 + cols_2;
  424.     lum2 = lum + cols_2 + cols_2;
  425.     for (y=0; y<rows; y+=2) {
  426.     for (x=0; x<cols_2; x++) {
  427.         int R, G, B;
  428.  
  429.         CR = *cr++;
  430.         CB = *cb++;
  431.         cr_r = Cr_r_tab[CR];
  432.         cr_g = Cr_g_tab[CR];
  433.         cb_g = Cb_g_tab[CB];
  434.         cb_b = Cb_b_tab[CB];
  435.  
  436.             L = L_tab[(int) *lum++];
  437.  
  438.         R = L + cr_r;
  439.         G = L + cr_g + cb_g;
  440.         B = L + cb_b;
  441.  
  442.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  443.  
  444. #ifdef INTERPOLATE
  445.             if(x != cols_2 - 1) {
  446.           CR = (CR + *cr) >> 1;
  447.           CB = (CB + *cb) >> 1;
  448.           cr_r = Cr_r_tab[CR];
  449.           cr_g = Cr_g_tab[CR];
  450.           cb_g = Cb_g_tab[CB];
  451.           cb_b = Cb_b_tab[CB];
  452.             }
  453. #endif
  454.  
  455.             L = L_tab[(int) *lum++];
  456.  
  457.         R = L + cr_r;
  458.         G = L + cr_g + cb_g;
  459.         B = L + cb_b;
  460.  
  461.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  462.  
  463.         /*
  464.          * Now, do second row.
  465.          */
  466.  
  467. #ifdef INTERPOLATE
  468.             if(y != rows - 2) {
  469.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  470.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  471.           cr_r = Cr_r_tab[CR];
  472.           cr_g = Cr_g_tab[CR];
  473.           cb_g = Cb_g_tab[CB];
  474.           cb_b = Cb_b_tab[CB];
  475.             }
  476. #endif
  477.  
  478.         L = L_tab [(int) *lum2++];
  479.         R = L + cr_r;
  480.         G = L + cr_g + cb_g;
  481.         B = L + cb_b;
  482.  
  483.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  484.  
  485.         L = L_tab [(int) *lum2++];
  486.         R = L + cr_r;
  487.         G = L + cr_g + cb_g;
  488.         B = L + cb_b;
  489.  
  490.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  491.     }
  492.     lum += cols_2 + cols_2;
  493.     lum2 += cols_2 + cols_2;
  494.     row1 += cols_2 + cols_2;
  495.     row2 += cols_2 + cols_2;
  496.     }
  497. }
  498.  
  499. /*
  500.  * Erik Corry's pixel doubling routines for 15/16/24/32 bit screens.
  501.  */
  502.  
  503.  
  504.  
  505. /*
  506.  *--------------------------------------------------------------
  507.  *
  508.  * Twox2Color16DitherImage --
  509.  *
  510.  *    Converts image into 16 bit color at double size.
  511.  *
  512.  * Results:
  513.  *    None.
  514.  *
  515.  * Side effects:
  516.  *    None.
  517.  *
  518.  *--------------------------------------------------------------
  519.  */
  520.  
  521. /*
  522.  * In this function I make use of a nasty trick. The tables have the lower
  523.  * 16 bits replicated in the upper 16. This means I can write ints and get
  524.  * the horisontal doubling for free (almost).
  525.  */
  526.  
  527. void
  528. Twox2Color16DitherImage(lum, cr, cb, out, rows, cols)
  529.   unsigned char *lum;
  530.   unsigned char *cr;
  531.   unsigned char *cb;
  532.   unsigned char *out;
  533.   int cols, rows;
  534.  
  535. {
  536.     int L, CR, CB;
  537.     unsigned int *row1 = (unsigned int *)out;
  538.     unsigned int *row2 = row1 + cols;
  539.     unsigned int *row3 = row2 + cols;
  540.     unsigned int *row4 = row3 + cols;
  541.     unsigned char *lum2;
  542.     int x, y;
  543.     unsigned int r, b, g;
  544.     int cr_r;
  545.     int cr_g;
  546.     int cb_g;
  547.     int cb_b;
  548.     int cols_2 = cols/2;
  549.  
  550.     lum2 = lum + cols_2 + cols_2;
  551.     for (y=0; y<rows; y+=2) {
  552.     for (x=0; x<cols_2; x++) {
  553.         int R, G, B;
  554.             int t;
  555.  
  556.         CR = *cr++;
  557.         CB = *cb++;
  558.         cr_r = Cr_r_tab[CR];
  559.         cr_g = Cr_g_tab[CR];
  560.         cb_g = Cb_g_tab[CB];
  561.         cb_b = Cb_b_tab[CB];
  562.  
  563.             L = L_tab[(int) *lum++];
  564.  
  565.         R = L + cr_r;
  566.         G = L + cr_g + cb_g;
  567.         B = L + cb_b;
  568.  
  569.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  570.         row1[0] = t;
  571.         row1++;
  572.         row2[0] = t;
  573.         row2++;
  574.  
  575. #ifdef INTERPOLATE
  576.             if(x != cols_2 - 1) {
  577.           CR = (CR + *cr) >> 1;
  578.           CB = (CB + *cb) >> 1;
  579.           cr_r = Cr_r_tab[CR];
  580.           cr_g = Cr_g_tab[CR];
  581.           cb_g = Cb_g_tab[CB];
  582.           cb_b = Cb_b_tab[CB];
  583.             }
  584. #endif
  585.             L = L_tab[(int) *lum++];
  586.  
  587.         R = L + cr_r;
  588.         G = L + cr_g + cb_g;
  589.         B = L + cb_b;
  590.  
  591.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  592.         row1[0] = t;
  593.         row1++;
  594.         row2[0] = t;
  595.         row2++;
  596.  
  597.         /*
  598.          * Now, do second row.
  599.          */
  600. #ifdef INTERPOLATE
  601.             if(y != rows - 2) {
  602.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  603.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  604.           cr_r = Cr_r_tab[CR];
  605.           cr_g = Cr_g_tab[CR];
  606.           cb_g = Cb_g_tab[CB];
  607.           cb_b = Cb_b_tab[CB];
  608.             }
  609. #endif
  610.         L = L_tab[(int) *lum2++];
  611.         R = L + cr_r;
  612.         G = L + cr_g + cb_g;
  613.         B = L + cb_b;
  614.  
  615.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  616.         row3[0] = t;
  617.         row3++;
  618.         row4[0] = t;
  619.         row4++;
  620.  
  621.         L = L_tab[(int) *lum2++];
  622.         R = L + cr_r;
  623.         G = L + cr_g + cb_g;
  624.         B = L + cb_b;
  625.  
  626.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  627.         row3[0] = t;
  628.         row3++;
  629.         row4[0] = t;
  630.         row4++;
  631.     }
  632.     lum += cols_2 + cols_2;
  633.     lum2 += cols_2 + cols_2;
  634.     row1 += 6 * cols_2;
  635.     row3 += 6 * cols_2;
  636.     row2 += 6 * cols_2;
  637.     row4 += 6 * cols_2;
  638.     }
  639. }
  640.  
  641.  
  642.  
  643.  
  644. /*
  645.  *--------------------------------------------------------------
  646.  *
  647.  * Twox2Color32 --
  648.  *
  649.  *    Converts image into 24/32 bit color.
  650.  *
  651.  * Results:
  652.  *    None.
  653.  *
  654.  * Side effects:
  655.  *    None.
  656.  *
  657.  *--------------------------------------------------------------
  658.  */
  659.  
  660. #ifdef SIXTYFOUR_BIT
  661. #define ONE_TWO 1
  662. #else
  663. #define ONE_TWO 2
  664. #endif
  665.  
  666. void
  667. Twox2Color32DitherImage(lum, cr, cb, out, rows, cols)
  668.   unsigned char *lum;
  669.   unsigned char *cr;
  670.   unsigned char *cb;
  671.   unsigned char *out;
  672.   int cols, rows;
  673.  
  674. {
  675.     int L, CR, CB;
  676.     unsigned long *row1 = (unsigned long *)out;
  677.     unsigned long *row2 = row1 + cols * ONE_TWO;
  678.     unsigned long *row3 = row2 + cols * ONE_TWO;
  679.     unsigned long *row4 = row3 + cols * ONE_TWO;
  680.     unsigned char *lum2;
  681.     int x, y;
  682.     unsigned int r, b, g;
  683.     int cr_r;
  684.     int cr_g;
  685.     int cb_g;
  686.     int cb_b;
  687.     int cols_2 = cols/2;
  688.  
  689.     lum2 = lum + cols_2 + cols_2;
  690.     for (y=0; y<rows; y+=2) {
  691.     for (x=0; x<cols_2; x++) {
  692.         int R, G, B;
  693.             long t;
  694.  
  695.         CR = *cr++;
  696.         CB = *cb++;
  697.         cr_r = Cr_r_tab[CR];
  698.         cr_g = Cr_g_tab[CR];
  699.         cb_g = Cb_g_tab[CB];
  700.         cb_b = Cb_b_tab[CB];
  701.  
  702.             L = L_tab[ (int) *lum++];
  703.  
  704.         R = L + cr_r;
  705.         G = L + cr_g + cb_g;
  706.         B = L + cb_b;
  707.  
  708.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  709.         row1[0] = t;
  710.         row2[0] = t;
  711. #ifndef SIXTYFOUR_BIT
  712.         row1[1] = t;
  713.         row2[1] = t;
  714. #endif
  715.         row1 += ONE_TWO;
  716.         row2 += ONE_TWO;
  717.  
  718. #ifdef INTERPOLATE
  719.             if(x != cols_2 - 1) {
  720.           CR = (CR + *cr) >> 1;
  721.           CB = (CB + *cb) >> 1;
  722.           cr_r = Cr_r_tab[CR];
  723.           cr_g = Cr_g_tab[CR];
  724.           cb_g = Cb_g_tab[CB];
  725.           cb_b = Cb_b_tab[CB];
  726.             }
  727. #endif
  728.             L = L_tab[ (int) *lum++];
  729.  
  730.         R = L + cr_r;
  731.         G = L + cr_g + cb_g;
  732.         B = L + cb_b;
  733.  
  734.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  735.         row1[0] = t;
  736.         row2[0] = t;
  737. #ifndef SIXTYFOUR_BIT
  738.         row1[1] = t;
  739.         row2[1] = t;
  740. #endif
  741.         row1 += ONE_TWO;
  742.         row2 += ONE_TWO;
  743.  
  744.         /*
  745.          * Now, do second row.
  746.          */
  747. #ifdef INTERPOLATE
  748.             if(y != rows - 2) {
  749.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  750.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  751.           cr_r = Cr_r_tab[CR];
  752.           cr_g = Cr_g_tab[CR];
  753.           cb_g = Cb_g_tab[CB];
  754.           cb_b = Cb_b_tab[CB];
  755.             }
  756. #endif
  757.         L = L_tab[ (int) *lum2++];
  758.         R = L + cr_r;
  759.         G = L + cr_g + cb_g;
  760.         B = L + cb_b;
  761.  
  762.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  763.         row3[0] = t;
  764.         row4[0] = t;
  765. #ifndef SIXTYFOUR_BIT
  766.         row3[1] = t;
  767.         row4[1] = t;
  768. #endif
  769.         row3 += ONE_TWO;
  770.         row4 += ONE_TWO;
  771.  
  772.         L = L_tab[(int) *lum2++];
  773.         R = L + cr_r;
  774.         G = L + cr_g + cb_g;
  775.         B = L + cb_b;
  776.  
  777.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  778.         row3[0] = t;
  779.         row4[0] = t;
  780. #ifndef SIXTYFOUR_BIT
  781.         row3[1] = t;
  782.         row4[1] = t;
  783. #endif
  784.         row3 += ONE_TWO;
  785.         row4 += ONE_TWO;
  786.     }
  787.     lum += cols_2 + cols_2;
  788.     lum2 += cols_2 + cols_2;
  789.  
  790.     row1 += ONE_TWO * 6 *cols_2;
  791.     row3 += ONE_TWO * 6 *cols_2;
  792.     row2 += ONE_TWO * 6 *cols_2;
  793.     row4 += ONE_TWO * 6 *cols_2;
  794.     }
  795. }
  796.  
  797.